package com.car.service.impl.insurance;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.dao.user.UserDao;
import com.car.entity.Insurance;

import com.car.dao.insurance.InsuranceMapper;

import com.car.entity.User;
import com.car.service.insurance.InsuranceService;
import com.car.util.ResultMsg;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InsuranceServiceImpl implements InsuranceService {

    @Autowired
    @Getter
    @Setter
    InsuranceMapper insuranceMapper;

    @Autowired
    UserDao userDao;

    @Override
    public Page selectLikeInsurance(Map<String, Object> map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Page page = new Page((pageNum-1)*pageSize,pageSize);

        Integer pageIndex = (pageNum-1)*pageSize;
        map.put("pageIndex",pageIndex);
        Integer insuranceCount = insuranceMapper.getInsuranceCount(map);
        //设置起始位置
        List list = insuranceMapper.selectLikeInsurance(map);
        //先设置总条数
        page.setTotal(insuranceCount);
        //设置每页大小
        page.setSize(pageSize);
        //设置当前页
        page.setCurrent(pageNum);
        //设置内容
        page.setRecords(list);
        return page;
    }

    @Override
    public ResultMsg addInsurance(Insurance insurance,String userName) {
        ResultMsg resultMsg = new ResultMsg();
        User addUser = userDao.getUserByName(userName);
        insurance.setUserId(addUser.getUserId());
        int flag = insuranceMapper.insert(insurance);
        if(flag>0){
            resultMsg.setMsg("添加成功");
        }else {
            resultMsg.setMsg("添加失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg delInsurance(Insurance insurance) {
        ResultMsg resultMsg = new ResultMsg();
        LambdaUpdateWrapper<Insurance> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(Insurance::getInsuranceId,insurance.getInsuranceId());
        wrapper.set(Insurance::getState,insurance.getState());
        int flag = insuranceMapper.update(null,wrapper);
        if(flag>0){
            resultMsg.setMsg("删除成功");
        }else {
            resultMsg.setMsg("删除失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg updateInsurance(Insurance insurance) {
        ResultMsg resultMsg = new ResultMsg();
        int flag = insuranceMapper.updateById(insurance);
        if(flag>0){
            resultMsg.setMsg("修改成功");
        }else {
            resultMsg.setMsg("修改失败");
        }
        resultMsg.setFlag(flag);
        return resultMsg;
    }

    @Override
    public ResultMsg selectById(Insurance insurance) {
        ResultMsg resultMsg = new ResultMsg();
        Insurance result = insuranceMapper.getInsuranceById(insurance);
        if(result!=null){
            resultMsg.setMsg("查询成功");
            resultMsg.setFlag(1);
            resultMsg.setEntityObj(result);
        }else {
            resultMsg.setMsg("查询失败");
            resultMsg.setFlag(0);
        }
        return resultMsg;
    }

}
